package org.mch.biz.loading;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;

class LoadingDialog extends JDialog implements Runnable {
	private static final long serialVersionUID = -4815022906912050971L;

	private JLabel labelInfo;
	private JLabel label;
	private JPanel panel;

	private JFrame frame;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoadingDialog dialog = new LoadingDialog();
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public LoadingDialog() {
		super();
		initGUI();
	}

	public LoadingDialog(JFrame frame, String labelInfo) {
		super(frame);
		this.frame = frame;
		initGUI();
		getLabelInfo().setText(labelInfo);
	}

	private void initGUI() {
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setUndecorated(true);
		setResizable(false);
		setModal(true);
		setBounds(100, 100, 331, 181);
		getContentPane().setLayout(new BorderLayout(0, 0));
		getContentPane().add(getPanel(), BorderLayout.CENTER);
	}

	public JLabel getLabelInfo() {
		if (labelInfo == null) {
			labelInfo = new JLabel();
			labelInfo.setPreferredSize(new Dimension(55, 32));
			labelInfo.setFont(new Font("宋体", Font.PLAIN, 13));
			labelInfo.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return labelInfo;
	}

	public JLabel getLabel() {
		if (label == null) {
			label = new JLabel("");
			label.setFont(new Font("Dialog", Font.PLAIN, 12));
			label.setBackground(new Color(255, 255, 255));
			label.setHorizontalTextPosition(SwingConstants.CENTER);
			label.setHorizontalAlignment(SwingConstants.CENTER);
			ImageIcon image = new ImageIcon(LoadingDialog.class.getResource("/resource/images/loading.gif"));

			// image.setImage(image.getImage().getScaledInstance(label.getFontMetrics(label.getFont()).getWidths()[0],
			// label.getFontMetrics(label.getFont()).getHeight(),
			// Image.SCALE_DEFAULT));

			label.setIcon(image);
		}
		return label;
	}

	public void showModel() {
		int screenWidth = this.frame.getWidth() / 2; // 获取屏幕的宽
		int screenHeight = this.frame.getHeight() / 2; // 获取屏幕的高

		int height = getHeight();
		int width = getWidth();

		// 移动TestFrame至居中位置
		this.setLocation(screenWidth - width / 2 + this.frame.getX(), screenHeight - height / 2 - 50 + this.frame.getY());
		this.setVisible(true);
	}

	@Override
	public void run() {
		showModel();
	}

	public JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setBorder(new LineBorder(Color.LIGHT_GRAY, 9));
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getLabelInfo(), BorderLayout.SOUTH);
			panel.add(getLabel(), BorderLayout.CENTER);
		}
		return panel;
	}
}
